package com.example.sxmall.controller;


import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.sxmall.common.dto.PercontentDto;
import com.example.sxmall.common.lang.Result;
import com.example.sxmall.dao.pojo.Admins;
import com.example.sxmall.dao.pojo.Percontent;
import com.example.sxmall.dao.pojo.PermPerc;
import com.example.sxmall.dao.pojo.Permission;
import com.example.sxmall.service.PermPercService;
import org.apache.ibatis.executor.ResultExtractor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhang
 * @since 2022-03-22
 */
@RestController
@RequestMapping("/percontent")
public class PercontentController extends BaseController {
    //获取用户信息
    public Result nav(Principal principal){
        //当前登陆用户信息
        Admins admins = adminsService.getByAdminsName(principal.getName());
        //获取权限信息
        String authorityInfo = adminsService.getAdminsAuthorityInfo(admins.getAid());
        String[] authorityInfoArray = StringUtils.tokenizeToStringArray(authorityInfo, ",");

        //获取导航栏信息
        List<PercontentDto> navs = percontentService.getCurrentAdminsNav();

        return Result.succ(
                MapUtil.builder()
                        .put("authoritys",authorityInfoArray)
                        .put("nav",navs)
                        .map()

        );
    }

    @PreAuthorize("hasAuthority('sys:pct:list')")
    @GetMapping("/info/{pcid}")
    public Result info(@PathVariable(name = "pcid") Integer pcid){

        return Result.succ(percontentService.getById(pcid));
    }

    @GetMapping("/list")
    @PreAuthorize("hasAuthority('sys:pct:list')")
    public Result list(Integer id){
        List<Percontent> percontents = percontentService.tree();
        return Result.succ(percontents);
    }

    @PostMapping("/save")
    @PreAuthorize("hasAuthority('sys:pct:save')")
    public Result save(@Validated @RequestBody Percontent percontent){
        percontentService.save(percontent);
        return Result.succ(percontent);
    }

    @PostMapping("/update")
    @PreAuthorize("hasAuthority('sys:pct:update')")
    public Result update(@Validated @RequestBody Percontent percontent){
        percontentService.updateById(percontent);
        //清理缓存
        adminsService.clearAdminAuthorityInfoByPerconId(percontent.getPcid());
        return Result.succ(percontent);
    }

    @PostMapping("delete/{pcid}")
    @PreAuthorize("hasAuthority('sys:pct:delete')")
    public Result delete(Integer pcid){
        //判断是否有子权限
        int count = percontentService.count(new QueryWrapper<Percontent>().eq("parentId",pcid));
        if (count > 0){
            return Result.fail("请先删除子权限");
        }
        //清除缓存
        adminsService.clearAdminAuthorityInfoByPerconId(pcid);

        percontentService.removeById(pcid);
        //同步删除中间关联表
        permPercService.remove(new QueryWrapper<PermPerc>().eq("pcid",pcid));

        return Result.succ("");
    }
}
